package org.algorithm.落雪.数组.三月.第三周;

import java.util.Arrays;

/**
 * @version 1.0
 * @date 2022/03/16 9:08 PM
 * @description:
 *
 *
 * url: https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/
 */
public class 最高频元素的频数 {

    public static void main(String[] args) {
        int[] nums = {1,4,8,13};
        System.out.println(maxFrequency(nums, 5));
    }

    public static int maxFrequency(int[] nums, int k){
        Arrays.sort(nums);
        int result = 1;
        int left = 0,right = 1;//左区间、右区间下边
        int count = 0;//次数
        while (right < nums.length) {
            //计算窗口中每一个值 变为 窗口中最后一个值 的次数 总和
            count = count + (nums[right] - nums[right-1]) * (right - left);

            while (count > k) {
                count = count - (nums[right] - nums[left]);
                left++;
            }
            result = Math.max(result,right - left + 1);
            right++;
        }
        return result;
    }
}
